﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using hcom;
using System.Data;

namespace hcms.Admin
{
	public partial class RedirectList : System.Web.UI.Page
	{
		public Model model = Global.model;
		public DataRowCollection rows;
		private hString hstr = new hString();
		protected void Page_Load(object sender, EventArgs e)
		{
			if (Page.IsPostBack)	// 页面提交
			{
	switch (cmd.Value)
	{
		case "delete":
			delete();
			break;

		case "add":
			add();
			break;

		case "edit":
			edit();
			break;

		default:
			throw (new Exception("不可识别的提交命令: " + cmd.Value));
	}
			}

			rows = model.getRedirectRows();
		}

		private void delete()
		{
			// 删除数据库
			string strsql = string.Format("delete hcms_Redirect WHERE (redOldUrl IN ('{0}'))",
				sel.Value.Replace("'", "''").Replace(",", "','"));	// 把id加上引号变成字符串形式
			model.da.Execute(strsql);
			ClientScript.RegisterClientScriptBlock(this.GetType(), "script", "alert('删除完毕。')", true);
		}

		void add()
		{
			string strsql = string.Format("insert into hcms_Redirect(redOldUrl, redNewUrl) " +
				"values('{0}', '{1}');",
				hstr.ToSQL(htmOldUrl.Value),
				hstr.ToSQL(htmNewUrl.Value)
			);
			model.da.Execute(strsql);
		}

		void edit()
		{
			string strsql = string.Format("UPDATE hcms_Redirect set redOldUrl='{0}', redNewUrl='{1}' where redOldUrl='{2}'",
				hstr.ToSQL(htmOldUrl.Value),
				hstr.ToSQL(htmNewUrl.Value),
				hstr.ToSQL(htmOUrl.Value)		// 修改之前的旧网址
			);
			model.da.Execute(strsql);
		}
	}
}